﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;



namespace FXnaMet
{

    /// <summary>
    /// 用于表示高空气象数据OQA文件
    /// </summary>
    [Serializable]
    public class MetUOQAFile:MetUpperAirFile
    {





        List<UOAQRow> _data = new List<UOAQRow>();

        public List<UOAQRow> Data
        {
            get { return _data; }
           
        }




        UOQAHeader _header = new UOQAHeader();

        /// <summary>
        /// 文件头信息
        /// </summary>
        public UOQAHeader Header
        {
            get { return _header; }
          
        }


        List<UOQASection> _sections=new List<UOQASection> ();

        public List<UOQASection> Sections
        {
          get { return _sections; }
         
        }




        public override bool ReadFile(string fname)
        {

            this._data.Clear();


            string[] split = new string[] { " " };

            using (StreamReader reader = new StreamReader(fname))
            {
                string line;


                //用于提高读取效率

                bool hasReadHeader = false;


                while ((line = reader.ReadLine()) != null)
                {

                    string[] array = line.Split(split, StringSplitOptions.RemoveEmptyEntries);
                    if (array.Length > 0)
                    {
                        string str = array[0];
                        if (!hasReadHeader)
                        {
                            if (str.Contains("%"))
                            {
                                if (array[1] != "UPPERAIR")
                                {
                                    throw new Exception("不是标准的高空OQA气象文件");
                                    //return;
                                }
                                continue;
                            }
                            if (str.Contains("@"))
                            {

                                this._header.Location = array[2];

                                this._header.GeoPosition.GeoLat.SetValueWithString(array[3]);

                                this._header.GeoPosition.GeoLong.SetValueWithString(array[4]);

                                this._header.TimeZone = Convert.ToInt32(array[5]);

                                this._header.Height = double.Parse(array[6]);




                                hasReadHeader = true;
                            }
                        }
                        else
                        {
                            if (str.Contains("*"))
                            {
                                continue;
                            }


                            ///过程长度控制
                            ///
                            if (array.Length == 2)
                            {

                                UOQASection section = new UOQASection();






                                section.DateTime = MetHelper.GetDateTime(array[0]);

                                section.NLayers = Convert.ToInt32(array[1]);

                                this._sections.Add(section);


                                continue;


                            }





                            UOAQRow urow = new UOAQRow();
                            urow.StationPressture = Convert.ToInt32(array[0]);
                            urow.Height = Convert.ToInt32(array[1]);
                            urow.DryTemperature = Convert.ToInt32(array[2]);
                            urow.DewTemperature = Convert.ToInt32(array[3]);
                            urow.WindDirection = Convert.ToInt32(array[4]);
                            urow.WindSpeed = Convert.ToInt32(array[5]);


                            //这里取得最后一个section，并未它添加条目

                            UOQASection sec = this._sections[this._sections.Count - 1];

                            sec.Rows.Add(urow);






                        }

                    }
                }




                return true;




            }
        }



        public override bool WriteFile(string fname)
        {


            FileStream fs = new FileStream(fname, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            if (this._header != null)
            {
                sw.WriteLine("*% UPPERAIR");
                sw.WriteLine("*@ LOCATION " + this.Header.Location + "  " + this._header.GeoPosition.GeoLat.GetValueWithString()+" " + this._header.GeoPosition.GeoLong.GetValueWithString()+" "+this._header.TimeZone.ToString ()+" "+this._header.Height.ToString ());
                sw.WriteLine("*      AUDIT     UATT  UAWS  UALR ");
                sw.WriteLine("*** EOH: END OF UPPERAIR QA HEADERS");

            }


            //开始写数据


            //需要注意到是，这里并没有启用字符串的标准格式宽度，之后可能需要修改，以满足其他软件读取的要求


            foreach (UOQASection section in this._sections)
            {


                sw.WriteLine(section.DateTime.ToString("yyMMddHH")+" "+section.NLayers.ToString ());

                foreach (UOAQRow urow in section.Rows)
                {
                    //sw.WriteLine


                    sw.Write("    ");
                    sw.Write(urow.StationPressture);
                    sw.Write("    ");
                    sw.Write(urow.Height);
                    sw.Write("    ");
                    sw.Write(urow.DryTemperature);
                    sw.Write("    ");
                    sw.Write(urow.DewTemperature);
                    sw.Write("    ");
                    sw.Write(urow.WindDirection);
                    sw.Write("    ");
                    sw.WriteLine(urow.WindSpeed);





                    
                }


                
            }





         
            return true;




        }



    }


}
